#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/socket.h>
#include "network.h"
#include "world/action.h"
#include "util/log.h"
#include "main.h"

int disc_countdown; // Ticks until we consider ourselves disconnected from the server
int pulse_countdown; // Ticks until we send a new pulse signal

Network the_network;

/* Funny callbacks! */
void (*notify_clientjoin) (int index) = 0;
void (*notify_playerjoin) (int index) = 0;
void (*notify_clientleave) (int index) = 0;
void (*notify_playerleave) (int index) = 0;

bool common_network_parse_string (char* to, char** from)
{
	char* begin = strchr(*from, '"')+1;
	char* end = strchr(begin, '"');
	strncpy(to, begin, end-begin);
	*(to+(end-begin)) = '\0';
	*from = end+1;
	return true; // FIXME: return false on parse error
}

// FIXME: use this function instead of straight strtol
//        for common usage pattern & encapsulation
bool common_network_parse_int (int* to, char** from)
{
	*to = strtol(*from, from, 10);
	return true; // FIXME: return false on parse error
}

bool common_network_parse_ulong (unsigned long* to, char** from)
{
	*to = strtoul(*from, from, 10);
	return true; // FIXME: return false on parse error
}
